/*-------------------------------------------------------------------
*
* Filename: wf_file_util.h
* Description: 
*     
*
* Version:1.0
* Create date: 10/10/2004
* Revise date: 03/22/2007
* Author: walter.fan@gmail.com
*--------------------------------------------------------------------*/
#ifndef __FILEUTILS_H_
#define __FILEUTILS_H_
#include "wf_define.h"
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

#ifdef _UNIX
	#define SEPARATOR '/'
#else
	#define SEPARATOR '\\'
#endif


typedef vector<string> csv_line_t;
typedef vector<csv_line_t> csv_lines_t;

class Csv 
{
// read and parse comma-separated values
// sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625
public:
	Csv(istream& fin = cin, string sep = ",") : 
		fin(fin), fieldsep(sep) {}

	int getline(string&);
	string getfield(u_int n);
	int getnfield() const { return nfield; }

  private:
	istream& fin;			// input file pointer
	string line;			// input line
	vector<string> field;	// field strings
	u_int nfield;				// number of fields
	string fieldsep;		// separator characters

	int split();
	int endofline(char);
	int advplain(const string& line, string& fld, u_int);
	int advquoted(const string& line, string& fld, u_int);
};

class Cwf_file_util
{
public:
    static bool IsFileExists( const char *path );
    static bool IsDirExists( const char *path );

    static int CopyFile(const char* szSrc,const char* szDest);
    
    static void WriteSymLink(const char *target,const char *linkname);
    static int CreateDir(const char * pDir);
    static bool IsThisKindFile(string strfile,const char* suffix);
    static int GetStatus(const char* pszCmd,const char* pszPid);


    static off_t GetFileSize(const char* szFile);
    static int GetFileName(const char *path, char *buffer, size_t size);
    static int GetDirName(const char *path, char *buffer, size_t size);

    static int file2msg(const char* filename,char *msg,int len)
    {
        ifstream ifile(filename);
        if(!ifile)
        {
            cout<<"Unable to read "<<filename<<endl;
            return -1;
        }    
        ostringstream buf;
        char ch;
        while ( buf && ifile.get( ch ))
            buf.put( ch );
        ifile.close();
        strncpy(msg, buf.str().c_str(),len);
        return 0;
    }

    static int file2msg(const char* filename,string& msg)
    {
        ifstream ifile(filename);
        if(!ifile)
        {
            cout<<"Unable to read "<<filename<<endl;
            return -1;
        }    
        ostringstream buf;
        char ch;
        while ( buf && ifile.get( ch ))
            buf.put( ch );
        ifile.close();
        msg=buf.str();
        return 0;
    }
    
    template<class T>
    static int msg2file(const char* filename,const T message,std::_Ios_Openmode mode=ios::out)
    {
        ofstream tmpfile(filename,mode);
        if(!tmpfile)
        {
            err_trace("msg2file::Unable to write "<<filename);
            return -1;
        }
        tmpfile<<message;
        tmpfile.close();
        return 0;
    }

	static int ParseCsv(char* pCsvFile,csv_lines_t& lines);
	static int mkdir_if_not_exists(const char * pDir);


};


#endif /*FILEUTILS_H_*/
